from time import sleep
from keyboard import KeyboardController
from mouse import MouseController
from capture import WindowCaptureAndSearch
from joy import XBox360GamepadController
from text_mask import process_image
capture = WindowCaptureAndSearch('The First Descendant')
keyboard = KeyboardController()
mouse = MouseController(p_x=capture.tl_x, p_y=capture.tl_y)
joy = XBox360GamepadController()


def pre_aim():
    """
        right_joystick_float的表示右摇杆的x轴和y轴的偏移量，负数是往左往下，正数是往右往上
        取值范围是-1到1，0表示不动
        重新装填乱射开始前，从这里调整初始瞄准位置
    """
    print('向上瞄准')
    joy.right_joystick_float(-0.1, 0.9)
    sleep(0.4)
    joy.right_joystick_float(0, 0.9)
    sleep(0.4)
    joy.right_joystick_float(0, 0)
    sleep(0.6)

def jump():
    joy.press_a()
    sleep(0.05)
    joy.release_a()

def last_dagger(delay=4, p_x=0,p_y=-0.3):
    print(f'射击{delay}秒')
    joy.lt_float(1)
    joy.rt_float(1)
    joy.right_joystick_float(p_x, p_y)
    sleep(delay)
    joy.right_joystick_float(0, 0)
    joy.rt_float(0)
    joy.lt_float(0)

def last_dagger_2(delay=5):
    print(f'射击{delay}秒')
    joy.lt_float(1)
    py = -0.2
    for s in range(delay):
        joy.rt_float(1)
        if s < 3:
            print('开始压枪')
            joy.right_joystick_float(0, py)
        sleep(1)
        keyboard.press_key('v')
        if s < 1:
            print('向下瞄准')
            mouse.move(-10,220)
        else:
            py = 0
            joy.right_joystick_float(0, 0)
        joy.rt_float(0)
        img = capture.take_screenshot()
        r = capture.ocr_screenshot(screenshot=img,region=[1590, 435, 170, 25],only_text=0)
        if any('秒后移动阿尔比恩' in t for t in r):
            print('重启任务')
            break
    joy.lt_float(0)


def deep(sec, delay=5):
    """
        识字速度、装填速度等各种因素影响，需要根据实际情况调整delay
        保证boss亮血条时，带有“重新装填乱射”效果的匕首刚刚开始射击，
    """
    print('会受ocr的速度影响')
    for s in range(sec - delay, 0, -1):
        keyboard.press_key('1')
        print(f'剩余{s}秒', end='\r')
        sleep(1)

    jump()
    sleep(0.5)
    jump()
    sleep(0.2)
    print('开大招，等落地')
    keyboard.press_key('z')
    sleep(2)
    pre_aim()

    print('触发重新装填乱射')
    mouse.left_click()
    sleep(0.8)
    keyboard.press_key('r')
    sleep(0.8)
    last_dagger()
    last_dagger_2(delay=10)


if __name__ == '__main__':
    while True:
        img = capture.take_screenshot()

        r = capture.ocr_screenshot(screenshot=img,region=[1590, 435, 170, 25],only_text=0)
        if any('秒后移动阿尔比恩' in t for t in r):
            print('重启任务')
            keyboard.press_key('r', delay=2)
            continue
        
        _img = capture.crop_image(img, [890, 715, 145, 30])
        _img = process_image(_img)
        r = capture.ocr_screenshot(screenshot=_img, only_text=0)
        print(f"检测倒计时ocr结果：{r}")
        if any('距离开始还' in t for t in r):
            print('检测开始剩余时间')
            text = [t for t in r if '距离开始还' in t][0]
            sec = int(''.join(filter(str.isdigit, text)))
            deep(sec)
            continue

        sleep(1)
